import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;

//This is a driver script
//Dependencies: xl.java and Webmethods.java
public class HybridFramework {

	WebDriver myFD = new FirefoxDriver();
	
	String vKW, vxPath, vTestData;
	String dURL, dSearchTerm, dEmail, dPassword, dSearchResults, dWrongEmailMsg, dWelcomeMsg;

	@Test
	public void myHFDriver() throws Exception{
	/*	System.setProperty("webdriver.ie.driver", "D:\\Downloads\\IEDriverServer.exe");
		WebDriver myFD = new InternetExplorerDriver();*/
		long timeTaken;
		Date timeStart, timeEnd;
		
		
		myFD.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		//Read the TD, TCs and TSs
		String[][] xTD, xTC, xTS;
		xTD = xl.readXL("D:/A16/Excel/HF Automation.xls", "Test Data");
		xTC = xl.readXL("D:/A16/Excel/HF Automation.xls", "Test Cases");
		xTS = xl.readXL("D:/A16/Excel/HF Automation.xls", "Test Steps");
		String xlResults = "D:/A16/Excel/HF Automation_results1.xls";
		
		for (int x=1; x<xTD.length; x++){ //Loop for test data
			if(xTD[x][8].equalsIgnoreCase("Y")){ //Checking is the test data for execution
		
				dURL = xTD[x][1];
				dSearchTerm = xTD[x][2];
				dEmail = xTD[x][3];
				dPassword = xTD[x][4];
				dSearchResults = xTD[x][5];
				dWrongEmailMsg = xTD[x][6];
				dWelcomeMsg = xTD[x][7];
				
					//Go to each row in the test case and identify which one to execute
					for(int i=1; i<xTC.length; i++){
							if(xTC[i][3].equalsIgnoreCase("Y")){
							xTC[i][4] = "Pass"; //Default assigning test case as a pass
							timeStart = new Date();
							for (int k=1; k<xTS.length; k++){
									if (xTC[i][0].equalsIgnoreCase(xTS[k][1])){
									
										vKW = xTS[k][4];
										vxPath = xTS[k][5];
										vTestData = xTS[k][6];
										String vError = "Pass";
										try {
											
											vTestData = getTDValue(vTestData);
											
										//Call the corresponding keyword functions
										if(vKW.equalsIgnoreCase("enterText")){
											Webmethods.enterText(myFD, vxPath, vTestData);
										}
										if(vKW.equalsIgnoreCase("enterTextbyid")){
											Webmethods.enterTextbyid(myFD, vxPath, vTestData);
										}
										if(vKW.equalsIgnoreCase("navigateBrowser")){
											Webmethods.navigateBrowser(myFD, vTestData);
										}
										if(vKW.equalsIgnoreCase("clickElementbyid")){
											Webmethods.clickElementbyid(myFD, vxPath);
										}
										if(vKW.equalsIgnoreCase("clickElement")){
											Webmethods.clickElement(myFD, vxPath);
										}
										if(vKW.equalsIgnoreCase("selectList")){
											Webmethods.selectList(myFD, vxPath, vTestData);
										}
										if(vKW.equalsIgnoreCase("checkCheckbox")){
											Webmethods.checkCheckbox(myFD, vxPath);
										}
										if(vKW.equalsIgnoreCase("uncheckCheckbox")){
											Webmethods.uncheckCheckbox(myFD, vxPath);
										}
										if(vKW.equalsIgnoreCase("readText")){
											vError = Webmethods.readText(myFD, vxPath);
											System.out.println("Test Step is: " + vError);
											
										}
										if(vKW.equalsIgnoreCase("verifyText")){
											vError = Webmethods.verifyText(myFD, vxPath, vTestData);
											System.out.println("Test Step is: " + vError);
										}
										if(vKW.equalsIgnoreCase("verifyTextbycss")){
											vError = Webmethods.verifyTextbycss(myFD, vxPath, vTestData);
											System.out.println("Test Step is: " + vError);
										}
										if(vKW.equalsIgnoreCase("verifyTextbyid")){
											vError = Webmethods.verifyTextbycss(myFD, vxPath, vTestData);
											System.out.println("Test Step is: " + vError);
										}
										if(vError.equalsIgnoreCase("Pass")){
											xTS[k][8] = "Pass";	
										}else
										{
											xTS[k][8] = "Fail";
											xTS[k][9] = "Error happened as the function returned Fail";
											xTC[i][4] = "Fail";
										}
										
										
										} catch(Exception e){
											System.out.println("Error has happened " + e);
											xTS[k][8] = "Fail";
											xTS[k][9] = "Error has happened " + e;
											xTC[i][4] = "Fail";
										}
									}
								}
							timeEnd = new Date();
							timeTaken = (timeEnd.getTime() - timeStart.getTime())/1000;
							xTC[i][5] = timeTaken + " Secs";
							
							} else {
								System.out.println("The test case " + i + " is ignored");
							}
					}
					//Write back the test status and error details into test step sheet
					xl.writeXL("D:/A16/Excel/HFAutomation_TS_Res"+xTD[x][0]+".xls", "TestStep Res", xTS);
					xl.writeXL("D:/A16/Excel/HFAutomation_TC_Res"+xTD[x][0]+".xls", "TestCase Res", xTC);
				}
		}
		myFD.quit();
	}

	private String getTDValue(String vTestData) {
		if(vTestData.equals("dURL")){
			vTestData = dURL;
		}
		if(vTestData.equals("dSearchTerm")){
			vTestData = dSearchTerm;
		}
		if(vTestData.equals("dEmail")){
			vTestData = dEmail;
		}
		if(vTestData.equals("dPassword")){
			vTestData = dPassword;
		}
		if(vTestData.equals("dSearchResults")){
			vTestData = dSearchResults;
		}
		if(vTestData.equals("dWrongEmailMsg")){
			vTestData = dWrongEmailMsg;
		}
		if(vTestData.equals("dWelcomeMsg")){
			vTestData = dWelcomeMsg;
		}
		return vTestData;
	}
	
	/*public void executeKW(String vKW, String vXP, String vTD){
		
	}*/
	
}